﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping; // Gefur okkur annotations til þess að skilgreina töflunöfn og dálkanöfn á klasa
using System.Linq;
using System.Text;

namespace DataAccessLayer
{
    [Table(Name = "StudentToClass")]
    public class StudentToClass
    {
        [Column(Name = "StudentID", IsPrimaryKey = true, AutoSync = AutoSync.Always)]
        public int StudentID { get; set; }
        [Column(Name = "ClassID", IsPrimaryKey = true, AutoSync = AutoSync.Always)]
        public int ClassID { get; set; }

        private EntitySet<Student> students;
        [Association(Storage = "students", ThisKey = "StudentID", OtherKey = "ID")]
        public EntitySet<Student> Students
        {
            get { return this.students; }
            set { this.students.Assign(value); }
        }

        private EntitySet<Class> classes;
        [Association(Storage="classes", ThisKey = "ClassID", OtherKey = "ClassNumber")]
        public EntitySet<Class> Classes
        {
            get { return this.classes; }
            set { this.classes.Assign(value); }
        }

        public StudentToClass()
        {
            this.students = new EntitySet<Student>();
            this.classes = new EntitySet<Class>();
        }

    }

    [Table(Name = "TeacherToClass")]
    public class TeacherToClass
    {
        [Column(Name = "TeacherID", IsPrimaryKey = true, AutoSync = AutoSync.Always)]
        public int TeacherID { get; set; }
        [Column(Name = "ClassID", IsPrimaryKey = true, AutoSync = AutoSync.Always)]
        public int ClassID { get; set; }

        private EntitySet<Teacher> teachers;
        [Association(Storage = "teachers", ThisKey = "TeacherID", OtherKey = "ID")]
        public EntitySet<Teacher> Teachers
        {
            get { return this.teachers; }
            set { this.teachers.Assign(value); }
        }

        private EntitySet<Class> classes;
        [Association(Storage = "classes", ThisKey = "ClassID", OtherKey = "ClassNumber")]
        public EntitySet<Class> Classes
        {
            get { return this.classes; }
            set { this.classes.Assign(value); }
        }

        public TeacherToClass()
        {
            this.classes = new EntitySet<Class>();
            this.teachers = new EntitySet<Teacher>();
        }
    }


    [Table(Name="Student")]
    public class Student
    {
        [Column(Name = "StudentID", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
        public int ID { get; set; }
        [Column(Name = "StudentName")] // Nafn dálks í gagnagrunni!
        public String Name { get; set; }
        [Column(Name = "StudentEmail")]
        public String Email { get; set; }
           

        public Student()
        {
        }

        public Student(string name, string email) : this()
        {
            this.Name = name;
            this.Email = email;
        }
    }

    [Table(Name="Teacher")]
    public class Teacher
    {
        [Column(Name = "TeacherID", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
        public int ID { get; set; }
        [Column(Name = "TeacherName")]
        public String Name { get; set; }
        [Column(Name = "TeacherEmail")]
        public String Email { get; set; }

        public Teacher()
        {
        }

        public Teacher(string name, string email) : this()
        {
            this.Name = name;
            this.Email = email;
        }
    }

    [Table(Name="Class")]
    public class Class
    {
        [Column(Name = "ClassID", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
        public int ClassNumber { get; set; }
        [Column]
        public String ClassName { get; set; }

        public IQueryable<Student> Students
        {
            get {                
                var result = from s in DAL.GetDataContext.Students
                             join s2c in DAL.GetDataContext.StudentsToClasses 
                                on s.ID equals s2c.StudentID
                             where s2c.ClassID == this.ClassNumber
                             select s;
                return result;
            }
        }

        public IQueryable<Teacher> Teachers
        {
            get
            {
                var result = from t in DAL.GetDataContext.Teachers
                             join t2c in DAL.GetDataContext.TeachersToClasses
                             on t.ID equals t2c.TeacherID
                             where t2c.ClassID == this.ClassNumber
                             select t;
                return result;
            }
        }

        public Class()
        {
        }

        public Class(String className) : this()
        {
            this.ClassName = className;
        }
    }

}
